<!-- PrivacySettings.vue -->
<template>
  <div class="privacy-settings">
    <div class="card">
      <div class="card-header">
        <h2>隐私设置</h2>
      </div>

      <div class="form-container">
        <div class="form-group">
          <label>公开我的个人信息</label>
          <div class="switch-container">
            <input
                type="checkbox"
                id="showProfile"
                v-model="settings.showProfile"
                class="switch-input"
            >
            <label for="showProfile" class="switch-label"></label>
          </div>
        </div>

        <div class="form-group">
          <label>公开我的帖子</label>
          <div class="switch-container">
            <input
                type="checkbox"
                id="showPosts"
                v-model="settings.showPosts"
                class="switch-input"
            >
            <label for="showPosts" class="switch-label"></label>
          </div>
        </div>

        <div class="form-group">
          <label>公开我的收藏</label>
          <div class="switch-container">
            <input
                type="checkbox"
                id="showFavorites"
                v-model="settings.showFavorites"
                class="switch-input"
            >
            <label for="showFavorites" class="switch-label"></label>
          </div>
        </div>

        <div class="form-group">
          <label>公开我的粉丝列表</label>
          <div class="switch-container">
            <input
                type="checkbox"
                id="showFollowers"
                v-model="settings.showFollowers"
                class="switch-input"
            >
            <label for="showFollowers" class="switch-label"></label>
          </div>
        </div>

        <div class="form-actions">
          <button
              class="btn-save"
              @click="saveSettings"
              :class="{ loading: saving }"
          >
            <span v-if="!saving">保存设置</span>
            <span v-else>保存中...</span>
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'PrivacySettings',
  data() {
    return {
      settings: {
        showProfile: true,
        showPosts: true,
        showFavorites: true,
        showFollowers: true
      },
      loading: false,
      saving: false
    }
  },
  created() {
    this.loadSettings();
  },
  methods: {
    async loadSettings() {
      const storedUser = localStorage.getItem('user');
      this.user = storedUser ? JSON.parse(storedUser) : null;
      const uid = storedUser ? JSON.parse(storedUser).uid : null;

      try {
        console.log('DEBUG:测试隐私数据')
        const response = await this.$axios.put(`/api/percenter/privacy`, {
          params: { uid }
        });
        console.log(response.data);
        this.settings = {
          showProfile: response.data.showProfile !== false,
          showPosts: response.data.showPosts !== false,
          showFavorites: response.data.showFavorites !== false,
          showFollowers: response.data.showFollowers !== false
        };
      } catch (error) {
        // this.$message.error('获取隐私设置失败');
        console.error(error);
      }
    },
    async saveSettings() {
      const uid = localStorage.getItem('uid');
      const payload = {
        uid,
        ...this.settings
      };
      try {
        await this.$http.put(`/percenter/privacy`, payload);
        this.$message.success('隐私设置已保存');
      } catch (error) {
        this.$message.error('保存失败');
      }
    }
  }
}
</script>

<style scoped>
.privacy-settings {
  padding: 20px;
}

.form-container {
  padding: 20px;
}

.form-group {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 0;
  border-bottom: 1px solid #e0e0e0;
}

.form-group label {
  color: #607d8b;
  font-weight: 500;
}

.switch-container {
  position: relative;
}

.switch-input {
  position: absolute;
  opacity: 0;
}

.switch-label {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 24px;
  background-color: #e0e0e0;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.3s;
}

.switch-label::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  background-color: white;
  border-radius: 50%;
  transition: all 0.3s;
}

.switch-input:checked + .switch-label {
  background-color: #1976d2;
}

.switch-input:checked + .switch-label::after {
  left: 28px;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

.btn-save {
  padding: 10px 20px;
  background-color: #1976d2;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.btn-save.loading {
  opacity: 0.7;
  cursor: not-allowed;
}
</style>